﻿app.controller('PrayerGroupController', ['$scope', 'GlobalService', 'fileUpload', 'ApplicationService', 'alertboxservice', function ($scope, GlobalService, fileUpload, ApplicationService, alertboxservice) {


    alert();

    GlobalService.GetCountry().success(function (data) {
        $scope.countries = data;
    });



    $scope.$watch('family.PresentCountry', function (newVal) {
        if (angular.isUndefined(newVal) == false) {
            GlobalService.GetStates(newVal).success(function (data) {
                $scope.states = data;
            });
        }
    });

    $scope.$watch('family.PermanentCountry', function (newVal) {
        if (angular.isUndefined(newVal) == false) {
            GlobalService.GetStates(newVal).success(function (data) {
                $scope.statesP = data;
            });
        }
    });

    $scope.$watch('family.IsSameAddress', function (newVal) {
        if (angular.isUndefined(newVal) == false) {
            if (newVal == true) {
                $scope.family.PermanentCountry = angular.copy($scope.family.PresentCountry);
                $scope.family.PermanentState = angular.copy($scope.family.PresentState);
                $scope.family.PermanentCity = angular.copy($scope.family.PresentCity);
                $scope.family.PermanentAddress = angular.copy($scope.family.PresentAddress);
                $scope.family.PermanentPincode = angular.copy($scope.family.PresentPinCode);
            }
            else {
                $scope.family.PermanentCountry = null;
                $scope.family.PermanentState = null;
                $scope.family.PermanentCity = null;
                $scope.family.PermanentAddress = null;
                $scope.family.PermanentPincode = null;
            }
        }
    });

    BindGrid();

    $scope.toggle = function (modelstate, id) {
        $scope.modelstate = modelstate;
        $scope.id = id;
        switch (modelstate) {
            case 'add':
                $scope.title = "Add Prayer Groups";
                $scope.family = null;
                break;
            case 'edit':
                $scope.title = "Edit Prayer Groups";
                url = "Members/GetMember";
                ApplicationService.Getdata(url, id).success(function (data) {
                    debugger;
                    $scope.family = data;
                });
                break;
        }
        $("#PrayerGroup").modal({ backdrop: 'static' });
    }

    function BindGrid() {
        url = "Members/GetMembers";
        ApplicationService.Getdatas(url).success(function (data) {
            $scope.list = data;
            $scope.currentPage = 1;
            $scope.entryLimit = 10;
            $scope.filteredItems = $scope.list.length;
            $scope.totalItems = $scope.list.length;
        });
    }

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
    };

    $scope.sort_by = function (predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };



    $scope.delete = function (data, dataid) {
        swal({
            title: "Are you sure?",
            text: "Your will not be able to recover " + data + " Details!",
            type: "warning",
            showCancelButton: true,
            cancelButtonClass: 'btn-outline btn-black mr-10',
            cancelButtonText: 'Cancel',
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Delete",
            closeOnConfirm: false
        }, function (status) {
            if (status) {
                url = "Members/DeleteMember";
                ApplicationService.DeleteData(url, dataid, data).success(function (data) {
                    if (data == "DSUS") {
                        swal("Deleted!", "Details deleted successfully", "success");
                    }
                    else {
                        swal("failed", "Unable to delete this details", "error");
                    }
                    BindGrid();
                });
            }
        });
    }

    $scope.save = function (modelstate, id) {

        $scope.buttonstate = true;
        var url;
        switch (modelstate) {
            case 'add':
                url = "Members/AddMember";
                break;
            case 'edit':
                url = "Members/UpdateMember";
                break;
        }
        var uploadUrl = "CommonDataService/DataService/UploadMemberShipforms";
        if (angular.isUndefined($scope.family.SoftCopy) == false) {
            file = $scope.family.SoftCopy;
            fileUpload.uploadFileToUrl(file, uploadUrl).then(function (data) {
                if (data.data != "Nofile") {
                    $scope.family.SoftCopy = angular.copy(data.data);
                }
                SavefamilyDetails(url);
            });
        }
        else {
            SavefamilyDetails(url);
        }
    }

    function SavefamilyDetails(url) {
        ApplicationService.Save($scope.family, url).success(function (result) {
            alertboxservice.ShowMsg(result, "Family ");
            $scope.buttonstate = false;
            BindGrid();
        });
    }

}]);